import wanlshop_config from '@/common/config/config';

const func = {
	upImage(func) {
		uni.chooseImage({
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				console.log('filepath', tempFilePaths[0])
				this.upload(tempFilePaths[0], func);
			},
			fail: err => {
				console.log('-=-=-=errimg', err)
			}
		});
	},
	upload(filePath, func) {
		uni.uploadFile({
			url: wanlshop_config.appurl + '/wanlshop/user/upload', //仅为示例，非真实的接口地址
			filePath: filePath,
			name: "file",
			success: (uploadFileRes) => {
				let data = JSON.parse(uploadFileRes.data)
				func instanceof Function && func(data)
				// console.log(JSON.parse(uploadFileRes.data));
				// let url = JSON.parse(uploadFileRes.data).data.url;
				// url = this.getImgUrl(url);
				// self.form.shop_img.push(url);
			},
			complete: () => {
				// uni.hideLoading()
			}
		});
	},
	isMobile: function(mobile) {
		return /^1[3-9]\d{9}$/.test(mobile)
	},
	bigImage(list, current) {
		wx.previewImage({
			urls: list, //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
			current: '', // 当前显示图片的http链接，默认是第一个
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	msg(msg = '', icon = 'none') {
		uni.showToast({
			title: msg,
			icon,
		})
	},
	showImg(url = '') {
		if (!url) return ''
		if (url.indexOf('http') === 0) return url
		// if (url.indexOf('/') === 0) url = url.slice(1)
		if (url[0] !== '/') url = '/' + url
		return wanlshop_config.cdnurl + url
	},
	copy(text) {
		uni.setClipboardData({
			data: text,
			success: res => {
				this.msg('复制成功')
			},
			fail: err => {
				console.log('-=-=-=err', err)
			}
		})
	},
	setPrevPage(key,data = {}){
		const pages = getCurrentPages();
		const prevPage = pages[pages.length - 2]; // 上一页实例
		prevPage.loadData({
			[key]:data
		});
	}
}

export default func